﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QList>

#include <QStandardItemModel>
#include <QModelIndex>
#include <QHostAddress>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

#define LOG_ROW_COUNT_MAX       20  //日志最大行数
class TcpServer;
class TcpClient;
class UdpSocket;
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    static QString getIpAndPortStr(const QHostAddress &ip, int port) { return QString("%1:%2").arg(ip.toString()).arg(port); }

    QByteArray formatHexString(const QByteArray &arg, int n = 2, const QString &ch = " ");

private slots:
    void onTcpClientIsOnline(bool isOnline);
    void onClientUpdateTcp();
    void onReadMsgTcp(const QString &ip, const QByteArray& msg);
    void onCNTUpdate(int send_len, int rev_len);

    void onTimerTcpSerTimeout();
    void onTimerUdpSerTimeout();
    void onTimerClientTimeout();
    void onSystemLog(const QString& log_msg);

private slots:
    void on_tabWidget_currentChanged(int index);

    void onBtnListenPortClicked();
    void onBtnStopListenPortClicked();
    void onBtnDisConnClicked();

    void onChkSentTimeToggled(bool checked);
    void onBtnSendClicked();
    void onBtnClearCntClicked();
    void onBtnClearRevDataClicked();

    void on_btnConnectSer_clicked();
    void on_btnDisconnectSer_clicked();

private:
    Ui::MainWindow *ui;
    QStandardItemModel *m_itemModelTcpSer, *m_itemModelUdpSer;

    QTimer *m_timerTcpSer, *m_timerUdpSer, *m_timerClient;
    TcpServer *m_tcpServer;
    TcpClient *m_tcpClient;
    UdpSocket *m_udpServer;
    UdpSocket *m_udpClient;

    QList<QString> getHostIpAddress();
    void loadTcpServer();
    void loadUdpServer();
    void loadClient();

    bool sendMsgTcpSer();
    bool sendMsgUdpSer();
    bool sendMsgTcpClient();
    bool sendMsgUdpClient();

};
#endif // MAINWINDOW_H
